package com.earnings.okhttputils.utils.ui.dialog;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.earnings.R;
import com.earnings.databinding.ItemHelpTypeBinding;
import com.earnings.okhttputils.utils.adapter.DataBindingBaseadapter;
import com.earnings.okhttputils.utils.adapter.DataBindingViewHolder;
import com.earnings.okhttputils.utils.bean.UserHelpTypeData;
import com.earnings.okhttputils.utils.view.FullStaggeredGridLayoutManager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;



/**
 * 问题分类
 */

public abstract class HelpTypeDialog {

    private Activity context;
    private PopupWindow popWindow = null;
private RecyclerView recyclerView;
    private DataBindingBaseadapter dataBindingBaseadapter;
    private View v,mView;
    public HelpTypeDialog(Context context, View v){
    this.context= (Activity) context;
        this.v=v;
        init();
    }

    private  void init() {
        if (popWindow == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mView = inflater.inflate( R.layout.dialog_help_type, null);
            recyclerView= (RecyclerView) mView.findViewById(R.id.recyclerview);


            popWindow = new PopupWindow(mView, ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,false);
          //popWindow.setFocusable(true);
           // popWindow.setOutsideTouchable(true);
            popWindow.setAnimationStyle(android.R.anim.fade_in);
            mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    popWindow.dismiss();
                }
            });

            /*
           ColorDrawable dw = new ColorDrawable();
            dw.setColor(Color.parseColor("#80000000"));
           popWindow.setBackgroundDrawable(dw);*/

        }
    }
    public void show(){
       // backgroundAlpha(0.5f);
        popWindow.showAsDropDown(v);

        popWindow.setOnDismissListener(new poponDismissListener());
    }
    class poponDismissListener implements PopupWindow.OnDismissListener{

        @Override
        public void onDismiss() {
            // TODO Auto-generated method stub
            //Log.v("List_noteTypeActivity:", "我是关闭事件");
            backgroundAlpha(1f);
        }

    }
    /**
     * 设置添加屏幕的背景透明度
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha)
    {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        context. getWindow().setAttributes(lp);
    }

       /*
 获取列表
   */

    Map<Integer,View> contMap;
    public void initData(final List<UserHelpTypeData> datas){
        contMap=new HashMap<Integer, View>();
        recyclerView.setLayoutManager(new FullStaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        dataBindingBaseadapter = new DataBindingBaseadapter<UserHelpTypeData, ItemHelpTypeBinding>(context, datas, R.layout.item_help_type) {

            @Override
            public void convert(DataBindingViewHolder holder, ItemHelpTypeBinding binding, UserHelpTypeData homeButton) {


                binding.setMsg(homeButton);

                final int position=holder.getLayoutPosition();
                if (position==0){
                    holder.getView(R.id.view).setBackgroundResource(R.drawable.bg_style_round);
                    ((TextView)holder.getView(R.id.text)).setTextColor(mView.getResources().getColor(R.color.white));
                   // ((TextView)holder.getView(R.id.info)).setTextColor(mView.getResources().getColor(R.color.white));
                }
                if(contMap.get(position)==null)
                    contMap.put(position,holder.getView(R.id.view));
            }


            @Override
            public void onItemClick(DataBindingViewHolder holder, UserHelpTypeData homeButton) {
                for (int i = 0; i <datas.size() ; i++) {
                    View view=  contMap.get(i);
                    view.setBackgroundResource(R.drawable.bg_white_round_style_line);
                    ((TextView)contMap.get(i).findViewById(R.id.text)).setTextColor(mView.getResources().getColor(R.color.style));
                    //((TextView)contMap.get(i).findViewById(R.id.info)).setTextColor(mView.getResources().getColor(R.color.textmiddle));

                }
                View view=holder.getView(R.id.view);
                view.setBackgroundResource(R.drawable.bg_style_round);
                ((TextView) view.findViewById(R.id.text)).setTextColor(view.getResources().getColor(R.color.white));

                onClick(datas.get( holder.getLayoutPosition() ).getCat_id(),((TextView) view.findViewById(R.id.text)).getText().toString());
                dismiss();
                //((TextView)view.findViewById(R.id.info)).setTextColor(view.getResources().getColor(R.color.white));

            }
        };

        recyclerView.setAdapter(dataBindingBaseadapter);
    }
    public abstract  void onClick(String id,String text);
    public void dismiss(){
        popWindow.dismiss();
    }
}
